/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t_cics_analyze_header.c                            */
/*  NOTE      =                                                    */
/*  DATE      = 2016/02/19 by songbb                               */
/*******************************************************************/

#include <CUnit/Basic.h>

#include "frcomm.h"

#include <cics/config.h>
#include <cics/cics_core.h>
#include <cics/cics_header.h>
#include <apmtest_comm.h>

static int __suite_init(void)
{
    return 0;
}

static int __suite_cleanup(void)
{
    return 0;
}

/**
 * 正常测试
 */
static void __case1(void)
{
    char *p_bin = (char *) "47617465007010000000000500000000000000000000000442415345000000001600027a680002434e0000000000000000000000000000";
    char arr_payload[65536] = {'\0'};

    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);

    CU_ASSERT_EQUAL(cics_header_detect(arr_payload, strlen(p_bin) / 2), APM_L7_IS_YES);
}

/**
 * 异常测试
 * 标志位错误
 */
static void __case2(void)
{
    char *p_bin = (char *) "47415445007010000000000500000000000000000000000442415345000000001600027a680002434e0000000000000000000000000000";
    char arr_payload[65536] = {'\0'};

    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);

    CU_ASSERT_EQUAL(cics_header_detect(arr_payload, strlen(p_bin) / 2), APM_L7_IS_UNKNOWN);
}

/**
 * 异常测试
 * 包长小于通用头长度
 */
static void __case3(void)
{
    char *p_bin = (char *) "476174650070100000000005000000000000000000000004424153";
    char arr_payload[65536] = {'\0'};

    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);

    CU_ASSERT_EQUAL(cics_header_detect(arr_payload, strlen(p_bin) / 2), APM_L7_IS_UNKNOWN);
}

int add_suite_cics_header_detect(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" cics_header_detect()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", __case1) == NULL ||
        CU_add_test(pSuite, "case2", __case2) == NULL ||
        CU_add_test(pSuite, "case3", __case3) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
